//
//  CouponViewController.m
//  EasyToo
//
//  Created by zhengzhe on 13-9-14.
//
//

#import "CouponViewController.h"
#import "CouponDoorViewController.h"
#import "NetworkAlertView.h"
#import "DeviceUtil.h"

@interface CouponViewController ()

@end

@implementation CouponViewController
{
    NSMutableArray *_couponArray;
    
    UISegmentedControl *_segment;
    CouponCoverView *_coverView;
    CouponListView *_listView;
    
    NSString *imgurl;
    
    BOOL _segFlag;
}

- (void)dealloc
{
    [_couponArray release];
    [_segment release];
    [_coverView release];
    [_listView release];
    
    _couponArray = nil;
    _segment = nil;
    _coverView = nil;
    _listView = nil;
    [super dealloc];
    
}



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationItem setTitle:@"优惠券"];
	// Do any additional setup after loading the view.
    
    //等待网络
    NetworkAlertView *networking = [NetworkAlertView shareView];
    [networking showNetView];
    
    //请求数据
    CouponManager *couponmanager = [CouponManager shareManager];
    couponmanager.delegate = self;
    [couponmanager getMerchants];
    
    [self initSegmentCtrl];
    
    [self initCoverView];
    [self initListView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//初始化SegmentCtrl
- (void)initSegmentCtrl
{
    float height = IOS7_OR_MORE?44.0:0.0f;
    _segment = [[UISegmentedControl alloc] initWithFrame:CGRectMake(0, height, [DeviceUtil getScreenWidth], 30)];
    
    [_segment setSegmentedControlStyle:UISegmentedControlStyleBar];
    [_segment setBackgroundColor:[UIColor lightGrayColor]];
    [_segment insertSegmentWithTitle:@"优惠券浏览" atIndex:0 animated:NO];
    [_segment insertSegmentWithTitle:@"列表展示" atIndex:1 animated:NO];
    
    [_segment addTarget:self action:@selector(onTabSelected:) forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:_segment];
}

-(void)onTabSelected:(id)sender
{
    UISegmentedControl *control = (UISegmentedControl *)sender;
    
    switch (control.selectedSegmentIndex) {
        case 0:
            [self addCoverView];
            [self removeListView];
            break;
        case 1:
            [self removeCoverView];
            [self addListView];
            break;
        default:
            break;
    }
    
}

//封面展示
- (void)initCoverView
{
    float height = IOS7_OR_MORE?44.0:0.0f;
    _coverView = [[CouponCoverView alloc] initWithFrame:CGRectMake(0, height + 30, [DeviceUtil getScreenWidth], [DeviceUtil getScreenHeight] - height - 90) andDelegate:self];
}
- (void)addCoverView
{
    if (_coverView)
    {
        _segFlag = 0;
        [self.view addSubview:_coverView];
    }
}
- (void)removeCoverView
{
    if (_coverView)
    {
        [_coverView removeFromSuperview];
    }
}

//列表展示
- (void)initListView
{
    float height = IOS7_OR_MORE?44.0:0.0f;
    _listView = [[CouponListView alloc] initWithFrame:CGRectMake(0, height + 30, [DeviceUtil getScreenWidth], [DeviceUtil getScreenHeight] - height - 90) andDelegate:self];
}
- (void)addListView
{
    if (_listView)
    {
        _segFlag = 1;
        [self.view addSubview:_listView];
    }
}
- (void)removeListView
{
    if (_listView)
    {
        [_listView removeFromSuperview];
    }
}


#pragma network
- (void)getMerchantsFinish:(BOOL)isSuccess
             merchantArray:(NSMutableArray*)merchantArray;
{
    //取消等待
    NetworkAlertView *networking = [NetworkAlertView shareView];
    [networking delView];
    
    _coverView.couponArray = merchantArray;
    [_coverView.carousel reloadData];
    
    _listView.couponArray = merchantArray;
    [self addListView];
}

#pragma click
- (void)coverDetailClick:(Merchant*)couponinfo
{
    //等待网络
    NetworkAlertView *networking = [NetworkAlertView shareView];
    [networking showNetView];
    
    imgurl = couponinfo.imageUrl;
    
    //请求数据
    CouponManager *couponmanager = [CouponManager shareManager];
    couponmanager.delegate = self;
    [couponmanager getShops:couponinfo.merchantId];
}

- (void)detailClick:(Merchant*)couponinfo
{
    //等待网络
    NetworkAlertView *networking = [NetworkAlertView shareView];
    [networking showNetView];
    
    imgurl = couponinfo.imageUrl;

    //请求数据
    CouponManager *couponmanager = [CouponManager shareManager];
    couponmanager.delegate = self;
    [couponmanager getShops:couponinfo.merchantId];
}

#pragma network to next
- (void)getShopsFinish:(BOOL)isSuccess
             shopArray:(NSMutableArray*)shopArray
{
    //取消等待
    NetworkAlertView *networking = [NetworkAlertView shareView];
    [networking delView];
    
    CouponDoorViewController *detailController = [[CouponDoorViewController alloc] init];
    detailController.shopInfos = shopArray;
    detailController.imgurl = imgurl;
    [self.navigationController pushViewController:detailController animated:YES];
    [detailController release];
}

@end




